#!/bin/bash

ROOT_PATH="$1"
docker_volumns_base_path="$2"
pull_online="$3"

# 如果变量为空，设置默认值
if [ -z "$docker_volumns_base_path" ]; then
  docker_volumns_base_path="$ROOT_PATH/volumes"
fi


# 导入镜像
load() {
  docker load < $ROOT_PATH/package/mysql/mysql.tar

  echo '【安装提示】mysql 镜像导入完成'
}

##放置挂载文件
mount() {
  if test ! -d "$docker_volumns_base_path/mysql" ;then
    mkdir -p $docker_volumns_base_path/mysql/init
    mkdir -p $docker_volumns_base_path/mysql/conf.d
    mkdir -p $docker_volumns_base_path/mysql/data
    mkdir -p $docker_volumns_base_path/mysql/log
    
    cp -rf $ROOT_PATH/mysql/initData/* $docker_volumns_base_path/mysql/init
    cp -f $ROOT_PATH/mysql/conf.d/my.cnf $docker_volumns_base_path/mysql/conf.d/
  fi

  echo '【安装提示】挂载完成'
}

#启动基础模块
start() {
   docker-compose -f $ROOT_PATH/docker-compose.yml up -d mysql

   echo '【安装提示】启动 mysqL 模块完成'
}

if [ "$pull_online" = "y" ]; then
  echo "【安装提示】在线安装..."
else
  echo "【安装提示】离线安装，开始导入镜像"
  load
fi

mount

start

echo '【安装提示】mysql 安装完成'
